<?php
class User_group_model extends CI_Model {

	function __construct() {
        parent::__construct();
    }
	
    
	public function find_by_id($id) {
		$this->db->where('id', $id);
		return $this->db->get('user_group')->row_array();
	}
	public function get_user_count ($group_id, $status = 1) {
		$sql = " select count(*) as total from user_group g  where g.status = " . $status . " and g.group_id=" . $group_id;
		$result = $this->db->query($sql)->row_array();
		return $result['total'];
	}
	public function get_admin_count ($group_id) {
		$sql = " select count(*) as total from user_group g  where g.is_group_admin=1 and g.group_id=" . $group_id;
		$result = $this->db->query($sql)->row_array();
		return $result['total'];
	}
	/**
	 * 判断用户是否某个group的成员
	 * @param int $user_id
	 * @param int $group_id
	 */
	public function is_member($user_id, $group_id) {
		$sql = " select count(*) as total from user_group g  where g.status=1 and g.group_id=" . $group_id . " and user_id=" . intval($user_id);
		$result = $this->db->query($sql)->row_array();
		return $result['total'] ? 1 : 0;
	}
	
/**
	 * 判断用户是否加入某个group
	 * @param int $user_id
	 * @param int $group_id
	 */
	public function is_joined($user_id, $group_id) {
		$sql = " select count(*) as total from user_group g  where  g.group_id=" . $group_id . " and user_id=" . intval($user_id);
		$result = $this->db->query($sql)->row_array();
		return $result['total'] ? 1 : 0;
	}
	/**
	 * 判断用户是否某个group的管理员
	 * @param int $user_id
	 * @param int $group_id
	 */
	public function is_group_admin($user_id, $group_id) {
		$sql = " select count(*) as total from user_group g  where is_group_admin=1 and g.group_id=" . $group_id . " and user_id=" . intval($user_id);
		$result = $this->db->query($sql)->row_array();
		return $result['total'] ? 1 : 0;
	}
	/**
	 * 群组内用户
	 * @param int $group_id
	 * @param int $offset
	 * @param int $limit
	 */
	public function find_users($group_id, $offset = 0, $limit = null, $status=1) {
		$sql = " select g.*,u.nickname from user_group g left join users u on g.user_id=u.id where g.status=" . $status . " and   g.group_id=" . $group_id;
		if (!empty($limit)) {
			$sql .= " limit " . $offset . ", " . $limit;
		}
		return $this->db->query($sql)->result_array();
	}
	
	/**
	 * 
	 * 获取群组的管理员
	 * @param int $group_id
	 */
	public function find_group_admins($group_id) {
		$sql = " select g.*,u.nickname from user_group g left join users u on g.user_id=u.id where g.status=1 and is_group_admin=1 and   g.group_id=" . $group_id;
		return $this->db->query($sql)->result_array();
	}
	/**
	 * 退出群组
	 * @param int $group_id
	 * @param int $user_id 
	 */
	public function delete($user_id, $group_id) {
		$sql = "delete from user_group where group_id = " . intval($group_id) . " and user_id=" . intval($user_id);
		$this->db->query($sql);
		return 1;
	}
	
	function remove($id, $group_id) {
		$this->db->where('id', $id);
		$this->db->where('group_id', $group_id);
		$this->db->delete('user_group'); 
	}
	/**
	 * 添加新用户到组
	 * @param array $data
	 */
	public function add($data) {
		$this->db->insert("user_group", $data);
		$this->db->insert_id();
		$sql = " update groups set member_count = member_count+1 where id=" . $data['group_id'];
		$this->db->query($sql);
		return;
	}
	
	public function set_as_admin($user_id, $group_id, $set = 1) {
		$sql = "update user_group set is_group_admin = " .$set ." where group_id = " . intval($group_id) . " and user_id=" . intval($user_id);
		$this->db->query($sql);
		
	}
	
	public function allow($id, $group_id, $set = 1) {
		$sql = "update user_group set status=1 where group_id = " . intval($group_id) . " and id=" . intval($id);
		$this->db->query($sql);
	}
}